# -*- coding: utf-8 -*-
# @Time    : 2024/8/28 16:51
# @Author  : floraachy
# @File    : repo_branch_page
# @Software: PyCharm
# @Desc:

# 标准库导入
# 第三方库导入
import allure
# 本地应用/模块导入
from utils.base_utils.base_page import BasePage


class RepoBranchPage(BasePage):
    """仓库分支列表页"""

    # 【新建分支】按钮
    locator_new_branch_btn = "//span[text()='+ 新建分支']"
    # "分支名称"输入框
    locator_branch_name_input = "//div[@class='ant-modal-body']//input[@placeholder='请输入分支名称']"
    # "选择分支起点"下拉框
    locator_choose_branch_div = "//div[text()='分支起点']/following-sibling::div"
    # 分支下拉值
    locator_source_branch_value = "//li[text()='{}']"
    # 【确认新建】按钮
    locator_confirm_new_branch_btn = "//span[text()='确认新建']/.."
    # "新建分支成功"/"删除成功"提示语
    locator_branch_success_tip = "//span[text()='{}']"
    # 列表指定分支名称
    locator_specified_branch_name = "//tbody[@class='ant-table-tbody']//a[text()='{}']"
    # 【删除】按钮
    locator_specified_branch_delete_btn = "//tbody[@class='ant-table-tbody']//a[text()='{}']/../following-sibling::td//button"
    # 【确认删除】按钮
    locator_confirm_delete_branch_btn = "//span[text()='确认删除']/.."
    # 分支切换
    locator_switch_branch_div = "//tbody[@class='ant-table-tbody']//a[text()='{}']/../following-sibling::td//div[@class='ant-select ant-select-enabled']"
    # 分支下拉值
    locator_branch_dropdown_value = "//li[text()='{}']"
    # "已删除分支"tab
    locator_deleted_branch_tab = "//span[text()='已删除分支']"
    # 【恢复分支】按钮
    locator_recover_branch_btn = "//td[@title='{}']/following-sibling::td//button"

    @allure.step("访问仓库分支列表页面：/{repo_owner}/{repo_identifier}/branches")
    def navigate(self, repo_owner, repo_identifier):
        """
        访问仓库分支列表页面
        """
        self.visit(f"/{repo_owner}/{repo_identifier}/branches")

    @allure.step("点击【新建分支】按钮")
    def click_new_branch_button(self):
        """点击【新建分支】按钮"""
        self.click(self.locator_new_branch_btn)

    @allure.step("点击【确认新建】按钮")
    def click_confirm_new_branch_button(self):
        """点击【确认新建】按钮"""
        self.click(self.locator_confirm_new_branch_btn)

    @allure.step("输入分支名称： {name}")
    def input_branch_name(self, name):
        """输入分支名称"""
        self.input(self.locator_branch_name_input, name)

    @allure.step("输入分支名称： {source_branch_name}")
    def choose_source_branch_name(self, source_branch_name):
        """选择分支起点"""
        self.click(self.locator_choose_branch_div)
        self.click(self.locator_source_branch_value.format(source_branch_name))

    @allure.step("指定分支：{name}， 点击【删除】按钮")
    def click_delete_specified_branch_button(self, name):
        """点击【删除】按钮"""
        self.click(self.locator_specified_branch_delete_btn.format(name))

    @allure.step("点击【确认删除】按钮")
    def click_confirm_delete_branch_button(self):
        """点击【确认删除】按钮"""
        self.click(self.locator_confirm_delete_branch_btn)

    @allure.step("切换分支{branch_name}为：{branch_type}")
    def switch_branch(self, branch_name, branch_type):
        """切换分支"""
        self.click(self.locator_switch_branch_div.format(branch_name))
        self.click(self.locator_branch_dropdown_value.format(branch_type))

    @allure.step("点击'已删除分支'tab")
    def click_deleted_branch_tab(self):
        """点击'已删除分支'tab钮"""
        self.click(self.locator_deleted_branch_tab)

    @allure.step("点击【恢复分支】按钮")
    def click_recover_branch_button(self, branch_name):
        """点击【恢复分支】按钮"""
        self.click(self.locator_recover_branch_btn.format(branch_name))
